package com.ctrip.demo.leetcode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RemoveListNode {

    public static void main(String[] args) {
        System.out.println(removeListNode(new ListNode(2,new ListNode(3,new ListNode(4,new ListNode(5,new ListNode(6))))), 2));

        getMapValue();
    }




    public static ListNode removeListNode(ListNode head, int n) {
        // 拆分链表
        ListNode temp = head;
        Map<Integer,ListNode> map = new HashMap<>();
        int index = 1;
        while (temp != null) {
            // 将拆分的链表存放在map里面
            map.put(index ++, temp);
            temp = temp.next;
        }

        System.out.println(map);
        int size = map.size();

        if (size == 1 || n == 0) {
            return null;
        }
        // 移除倒数第n个数
        if (n == size) {
            map.get(size - 1).next = null;
        } else if (n == 1) {
            head = head.next;
        } else {
            map.get(size - n).next = map.get(size-n + 2);
        }

        return head;
    }



    public static ListNode removeNthFromEnd(ListNode head, int n) {

        ListNode temp = head;
        Map<Integer,ListNode> m = new HashMap<Integer,ListNode>();
        int index=1;
        while(temp!=null){
            m.put(index++,temp);
            temp = temp.next;
        }
        int size = m.size();
        if(size == 1) {
            return null;
        }
        if(n == 1){
            m.get(size-1).next = null;
        }else if(n == size){
            head = head.next;
        }else{
            m.get(size-n).next = m.get(size-n+2);
        }
        return head;

    }

    public static void getMapValue() {
        Map<Integer,Integer> map = new HashMap<>();
        map.put(1,10);
        map.put(2,20);
        map.put(3,30);
        map.put(4,40);
        System.out.println(map.get(map.size()));
    }

}
